<%= include ic/admin-header.html %>

<div class="am-cf admin-main">
  <%= include ic/admin-sidebar.html %>

  <!-- content start -->
  <div class="admin-content">

    <div class="am-cf am-padding">
      <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">评论</strong> / <small>Comment</small></div>
    </div>

    <div class="am-g">
      <div class="am-u-sm-12 am-u-md-6">
        <div class="am-btn-toolbar">
          <div class="am-btn-group am-btn-group-xs">
            <button type="button" class="am-btn am-btn-success"  onclick="setAll('open')"><span class="am-icon-check-circle-o"></span> 恢复</button>
            <button type="button" class="am-btn am-btn-danger"  onclick="setAll('ban')"><span class="am-icon-ban"></span> 禁用</button>
            <button type="button" class="am-btn am-btn-default" onclick="removeAll()"><span class="am-icon-trash-o"></span> 删除</button>
          </div>
        </div>
      </div>
      <div class="am-u-sm-12 am-u-md-3">
        <div class="am-form-group am-input-group-xs am-fl">
          <select id="param-enabled" data-am-selected="{btnSize: 'sm'}">
            <option value="all" <% if(typeof(param['enabled'])!="boolean"){ %>selected<% } %>>全部状态</option>
            <option value=false  <% if(typeof(param['enabled'])=="boolean" && param['enabled']==false){ %>selected<% } %>>已禁用</option>
            <option value=true  <% if(typeof(param['enabled'])=="boolean" && param['enabled']==true){ %>selected<% } %>>正常</option>
          </select>
        </div>
      </div>
      <div class="am-u-sm-12 am-u-md-3">
        <div class="am-input-group am-input-group-sm">
          <input type="text" class="am-form-field" id="search">
          <span class="am-input-group-btn">
            <button class="am-btn am-btn-default" onclick="search()" type="button">搜索</button>
          </span>
        </div>
      </div>
    </div>

    <div class="am-g">
      <div class="am-u-sm-12">
          <table class="am-table am-table-striped am-table-hover table-main">
            <thead>
              <tr>
                <th class="table-check"><input type="checkbox" onclick="checkAll(this)" /></th><th class="table-enabled">状态</th><th class="table-title">用户</th><th class="table-cacheName">标题</th><th class="table-date am-hide-sm-only">相关</th><th class="table-date am-hide-sm-only">创建日期</th><th class="table-set">操作</th>
              </tr>
          </thead>
          <tbody>
          	<% for(var i=0;i<pager.pagesize;i++){ %>
                <% if(i<results.length){ %>
                <tr>
                  <td><input value="<%= results[i].id %>" type="checkbox" /></td>
                  <td><% if(results[i].get("enabled")){ %> 正常 <% }else{ %> 屏蔽 <% } %></td>
                  <td><a><% if(typeof(results[i].get("fromUser"))=="undefined"){ %> - <% }else{ %><%= results[i].get("fromUser").get("displayName") %><% } %></a> 对 <a><% if(typeof(results[i].get("toUser"))=="undefined"){ %> - <% }else{ %><%= results[i].get("toUser").get("displayName") %><% } %></a> 说：</td>
                  <td><%= results[i].get('content') %></td>
                  <td><%= results[i].get('type') %>:<a href="#"><% if(results[i].get('type')=="image" && typeof(results[i].get("image"))!="undefined"){ %><%= results[i].get("image").get('name') %><% }else if(results[i].get('type')=="new" || results[i].get('type')=="trip"){ %><%= results[i].get(results[i].get('type')).get('title') %><% } %></a></td>
                  <td class="am-hide-sm-only"><%= moment(results[i].createdAt).zone(-8).format('YYYY年MM月DD日 HH:mm:ss') %></td>
                  <td>
                    <div class="am-btn-toolbar">
                      <div class="am-btn-group am-btn-group-xs">
                        <div class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only" onclick="deleteOne('<%= results[i].id %>')"><span class="am-icon-trash-o"></span> 删除</div>
                      </div>
                    </div>
                  </td>
                </tr>
                <% }else{ %>
                <tr>
                  <td colspan="7">&nbsp;</td>
                </tr>
                <% } %>
            <% } %>
          </tbody>
        </table>
        <hr />
		<%= include ic/admin-pager.html %>
		<hr />
        <p></p>
      </div>

    </div>
  </div>
  <!-- content end -->
</div>
<a class="am-icon-btn am-icon-th-list am-show-sm-only admin-menu" data-am-offcanvas="{target: '#admin-offcanvas'}"></a>
<style>
#vld-tooltip {position:absolute;z-index: 1200;padding: 5px 10px;background: #F37B1D;min-width: 150px;color: #fff;transition: all 0.15s;box-shadow: 0 0 5px rgba(0,0,0,.15);display: none;}
#vld-tooltip:before {position: absolute;top: -8px;left: 50%;width: 0;height: 0;margin-left: -8px;content: "";border-width: 0 8px 8px;border-color: transparent transparent #F37B1D;border-style: none inset solid;}
</style>
<%= include ic/admin-footer.html %>
<script type="text/javascript">
var param       = {},enabledReady=false,checked=false;
$(function() {
	$("#collapse-nav").addClass("am-in");
  <% for(var key in param){ %>
    <% if(typeof(param[key])=="string"){ %>
    param['<%= key %>']="<%= param[key] %>";
    <% }else{ %>
    param['<%= key %>']=<%= param[key] %>;
    <% } %>
  <% } %>
  $("#param-enabled").change(function(){
    if(enabledReady){
      goSearch("enabled");
    }else{
      enabledReady=true;
    }
  });
});
function checkAll(obj){
  if(!checked){
    checked=true;
    $("tbody input[type=checkbox]:not(:checked)").click();
  }else{
    checked=false;
    $("tbody input[type=checkbox]:checked").click();
  }
}

function goSearch(type){
  var url         = window.location.href;
  var arr         = url.split('?');
  var basicurl    = arr[0]+"?";
  var val         = $("#param-"+type).val();
  var urlParam    = "<%- urlParam %>";
  if(urlParam.indexOf(type+"=")==-1){
    //console.log(basicurl+"&"+type+"="+val);
    window.location.href=basicurl+"&"+type+"="+val;
  }else{
    window.location.href=basicurl
      +urlParam.replace(type+"="+param[type],type+"="+val);
  }
}

function getIds(){
  //console.dir($(".gallery-list .li.c"));
  return $("tbody input:checked").map(function(n,obj){
    //console.log($(obj).html());
    return $(obj).val();
  }).get().join(',');
}

function removeAll(){
  var ids=getIds();
  if(ids.length==0){
    mAlert({
      title:"错误",
      text:"未选择操作对象",
      onConfirm:function(e) {}
    });
  }else{
    var $Confirm = $("#my-confirm");
  	var $confirmBtn = $Confirm.find('[data-am-modal-confirm]');
  	$confirmBtn.one('click', function() {
  		$.post("delete", { "ids": ids,"ajax":true },function(data){
  			if(data){
  				if(data && data.success){
            window.location.href=window.location.href;
  				}else{
  					mAlert({
  						title:"错误",
  						text:data.error
  					});
  				}
  			}else{
  				mAlert({
  					title:"错误",
  					text:"操作失败！"
  				});
  			}
  		}, "json");
  	});

  	$Confirm.find(".text").text("你确定要删除这些数据吗？");
  	if($Confirm.is(".am-modal-out")){
  		$Confirm.modal('open');
  	}else{
  		$Confirm.modal({relatedTarget: this});
  	}
  }
}

function setAll(type){
  var ids=getIds();
  console.log(ids);
  if(ids.length==0){
    mAlert({
      title:"错误",
      text:"未选择操作对象",
      onConfirm:function(e) {}
    });
  }else{
    var $Confirm = $("#my-confirm");
  	var $confirmBtn = $Confirm.find('[data-am-modal-confirm]');
  	$confirmBtn.one('click', function() {
  		$.post("set", { "ids": ids,"ajax":true,type:type },function(data){
  			if(data){
  				if(data && data.success){
            window.location.href=window.location.href;
  				}else{
  					mAlert({
  						title:"错误",
  						text:data.error
  					});
  				}
  			}else{
  				mAlert({
  					title:"错误",
  					text:"操作失败！"
  				});
  			}
  		}, "json");
  	});

    if(type=="open"){
      $Confirm.find(".text").text("你确定要恢复这些数据吗？");
    }else if(type=="ban"){
      $Confirm.find(".text").text("你确定要禁用这些数据吗？");
    }

  	if($Confirm.is(".am-modal-out")){
  		$Confirm.modal('open');
  	}else{
  		$Confirm.modal({relatedTarget: this});
  	}
  }
}
</script>
